package com.hzit.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class FileService {

    private final String uploadDir = "src/main/resources/static/images"; // 图片存储路径

    public String uploadFile(MultipartFile file) {
        // 确保目录存在
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        String originalFileName = file.getOriginalFilename();

        String newFileName = UUID.randomUUID().toString() + "_" + originalFileName;

        try {
            File destFile = new File(uploadDir, newFileName);
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return "/static/images/" + newFileName;
    }
}
